package cn.kogee.basic.service.impl;

import cn.kogee.basic.mapper.BaseMapper;
import cn.kogee.basic.query.BaseQuery;
import cn.kogee.basic.service.IBaseService;
import cn.kogee.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 业务实现类：部门
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.loadById(id);
    }

    @Transactional
    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    @Transactional
    @Override
    public void del(Long id) {
        baseMapper.remove(id);

    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //先查询总数量
        Integer count = baseMapper.queryCount(baseQuery);
        if(count == null || count == 0){//没有数据
            return  new PageList<>();
        }
        //再查询当前页的分页数据
        List<T> data = baseMapper.queryData(baseQuery);

        return new PageList<>(count,data);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void bathremove(List<Long> ids) {
        baseMapper.bathremove(ids);
    }
}
